package com.sutuijingling.a;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.google.gson.f;
import com.sutuijingling.bean.AccountBean;
import com.sutuijingling.bean.SearchBean;
import com.sutuijingling.bean.TopBean;
import com.sutuijingling.bean.j;
import com.sutuijingling.result.c;
import com.sutuijingling.result.h;
import com.sutuijingling.result.i;
import com.sutuijingling.result.k;
import com.sutuijingling.result.n;
import com.sutuijingling.result.o;
import com.sutuijingling.result.p;
import com.sutuijingling.result.q;
import com.sutuijingling.result.r;
import com.sutuijingling.result.s;
import com.sutuijingling.result.t;
import com.sutuijingling.result.u;
import com.sutuijingling.result.w;
import com.sutuijingling.utils.NumberUtil;
import com.sutuijingling.utils.a;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import org.apache.http.HttpStatus;

public class b {
    static t a = new t(2);
    public static String[] b = new String[]{"衬衫", "牛仔裤", "跑步鞋"};
    public static Random c = new Random();
    static File e = new File("/data/local/tmp/vpn.txt");
    static String f = "/data/data/jp.jun_nama.test.utf7ime/files/userid.txt";
    static String g = "/data/data/jp.jun_nama.test.utf7ime/files/again";
    static String h = "/data/data/jp.jun_nama.test.utf7ime/files/finishnum.txt";
    private static b i;
    f d = new f();
    private String j;
    private String k;
    private long l = 0;
    private UiDevice m;

    public static b a() {
        return a(null);
    }

    public static b a(UiDevice uiDevice) {
        if (i == null) {
            i = new b();
            b bVar = i;
            String c = c();
            com.sutuijingling.utils.b.b(" key :" + c, new Object[0]);
            i.a(c);
            i.b(uiDevice);
        }
        return i;
    }

    public static void a(int i) {
        CharSequence valueOf = String.valueOf(i);
        String str = "/data/data/jp.jun_nama.test.utf7ime/files/nomission.txt";
        try {
            File file = new File(str);
            if (!file.exists()) {
                file.createNewFile();
                a.b(" chmod 777 " + str);
            }
            com.sutuijingling.utils.b.b(" write content :" + i + " to file :" + file, new Object[0]);
            org.apache.commons.io.b.a(file, valueOf);
            file.setReadable(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void a(TopBean topBean, c cVar) throws UiObjectNotFoundException, c {
        SearchBean searchBean = topBean.getSearchBean();
        AccountBean f = f();
        if (f == null) {
            f = topBean.getAcc();
        }
        f.decrypt(this.k);
        searchBean.setMaxPrice("");
        searchBean.setMinPrice("");
        searchBean.setCity("");
        searchBean.setProvince("浙江");
        searchBean.setKeyword("纱裙");
        searchBean.setTitle("网纱裙半身裙长裙韩版高腰A字裙大摆沙滩裙春夏百搭百褶蓬蓬裙子");
        searchBean.setShopName("桃花源A区");
        searchBean.setMainItemSeconds(3);
        searchBean.setFreeDelivery(false);
        searchBean.setGoldMoney(false);
        searchBean.setSalenumfirst(false);
        searchBean.setZhiTongChe(false);
        searchBean.setTitle1("卖疯了!超轻羽毛球拍单拍正品全碳素碳纤维");
        searchBean.setTitle2("卖疯了!超轻羽毛球拍单拍正品全碳素碳纤维");
        searchBean.setAddToCart(false);
        searchBean.setViewMark(1);
        searchBean.setTKLItem(false);
        searchBean.setTklItemUrl("http://b.mashort.cn/h.YddS0?sm=518600");
        searchBean.setZhanXian(false);
        System.out.println(" trim result :" + f);
        topBean.setAcc(f);
        i iVar = new i(topBean, this.m);
        if (cVar.a()) {
            iVar.a(f);
            return;
        }
        u.a().a(this.l, j.order);
        try {
            iVar.searchParams.setWeitao(false);
            iVar.searchParams.setTklItemUrl("复制这条信息，打开手机淘宝 即可看到【【带你走入包包的梦幻旅程 #牛牛包包正品#】(分享自微淘 牛牛包包正品店 的广播)】￥AAEPZgMZ￥");
            iVar.c(0);
            f(this.m);
        } catch (Exception e) {
            e.printStackTrace();
        }
        u.a().b();
    }

    public static void b(String str) {
        String str2 = "/data/data/jp.jun_nama.test.utf7ime/files/acc.txt";
        try {
            File file = new File(str2);
            if (!file.exists()) {
                file.createNewFile();
                a.b(" chmod 777 " + str2);
            }
            com.sutuijingling.utils.b.b(" write content :" + str + " to file :" + file, new Object[0]);
            org.apache.commons.io.b.a(file, (CharSequence) str);
            file.setReadable(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String c() {
        try {
            File file = new File("/data/data/jp.jun_nama.test.utf7ime/files/key.txt");
            return !file.exists() ? "123456" : org.apache.commons.io.b.b(file).trim();
        } catch (Exception e) {
            e.printStackTrace();
            return "654321";
        }
    }

    public static boolean d() {
        boolean z = false;
        File file = new File("/data/local/tmp/storeup.txt");
        if (!file.exists()) {
            return z;
        }
        try {
            return Boolean.valueOf(org.apache.commons.io.b.b(file).trim()).booleanValue();
        } catch (IOException e) {
            e.printStackTrace();
            return z;
        }
    }

    public static boolean e() {
        boolean z = false;
        File file = new File("/data/local/tmp/shopstore");
        if (!file.exists()) {
            return z;
        }
        try {
            return Boolean.valueOf(org.apache.commons.io.b.b(file).trim()).booleanValue();
        } catch (IOException e) {
            e.printStackTrace();
            return z;
        }
    }

    public static AccountBean f() {
        try {
            File file = new File("/data/data/jp.jun_nama.test.utf7ime/files/acc.txt");
            if (!file.exists()) {
                return null;
            }
            String trim = org.apache.commons.io.b.b(file).trim();
            return org.apache.commons.lang.c.a(trim) ? null : (AccountBean) new f().a(trim, AccountBean.class);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private long j() {
        String replace = a.a(f).trim().replace("%0A", "");
        return org.apache.commons.lang.c.a(replace) ? 0 : Long.parseLong(replace);
    }

    private void k() {
        int i = 0;
        try {
            File file = new File(h);
            if (file.exists()) {
                String replace = a.a(h).trim().replace("%0A", "");
                if (!org.apache.commons.lang.c.a(replace)) {
                    i = Integer.parseInt(replace);
                }
                if (i <= 0) {
                    a.b(" chmod 777 " + h);
                }
            } else {
                file.createNewFile();
                a.b(" chmod 777 " + h);
            }
            CharSequence valueOf = String.valueOf(i + 1);
            com.sutuijingling.utils.b.b(" write content :" + valueOf + " to file :" + file, new Object[0]);
            org.apache.commons.io.b.a(file, valueOf);
            file.setReadable(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public TopBean a(Long l) throws c {
        return TopBean.parseFromJson((String) new h(c(), l).c(), this.k);
    }

    public void a(long j, String str) throws UiObjectNotFoundException, c {
        try {
            TopBean topBean = new TopBean();
            topBean.setAcc(new AccountBean());
            SearchBean searchBean = new SearchBean();
            searchBean.setKeyword(b[c.nextInt(b.length)]);
            if (c.nextInt(10) % 2 == 0) {
                searchBean.setCity("上海");
            }
            searchBean.setMinPrice("100");
            searchBean.setMaxPrice("1000");
            searchBean.setMainItemSeconds(5);
            searchBean.setFreeDelivery(true);
            searchBean.setViewMark(177);
            topBean.setSearchBean(searchBean);
            i iVar = new i(topBean, this.m);
            iVar.K();
            iVar.J();
            new n(this.k, Long.valueOf(j)).c();
            f(this.m);
        } catch (Exception e) {
            e.printStackTrace();
            throw new c(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        }
    }

    public void a(UiDevice uiDevice, c cVar) throws c, UiObjectNotFoundException {
        System.out.println("curr dev [" + this.m + "] and input dev[" + uiDevice + "]");
        if (this.m == null) {
            this.m = uiDevice;
        }
        System.out.println("curr dev [" + this.m + "] and input dev[" + uiDevice + "]");
        if (org.apache.commons.lang.c.a(this.k)) {
            this.k = c();
        }
        System.out.println("read key:" + this.k);
        long g = g(uiDevice);
        if (this.l <= 0) {
            this.l = g;
        }
        String str = (String) new i(this.k, true).c();
        System.out.println(" pull task :" + str + "  with mission id :" + g);
        if (str == null) {
            a(uiDevice, str, cVar);
        } else {
            a(uiDevice, str, cVar);
        }
    }

    public void a(UiDevice uiDevice, Exception exception) {
        a(uiDevice, exception, 0);
    }

    public void a(UiDevice uiDevice, Exception exception, long j) {
        a(uiDevice, exception, j, 0, exception.getMessage(), "");
    }

    public void a(UiDevice uiDevice, Exception exception, long j, int i, String str, String str2) {
        String str3 = "errorImg";
        System.out.println(" key :" + this.k);
        try {
            if (j.netflow.name().equals(this.j)) {
                new q(Long.valueOf(j), i, this.k, str, null, str2).c();
                return;
            }
            new com.sutuijingling.result.j(Long.valueOf(j), this.k, null).c();
        } catch (c e) {
            e.printStackTrace();
        }
    }

    public void a(UiDevice uiDevice, String str) throws UiObjectNotFoundException, c {
        a(uiDevice, str, new c());
    }

    public void a(UiDevice uiDevice, String str, c cVar) throws UiObjectNotFoundException, c {
        TopBean parseFromJson = TopBean.parseFromJson(str, this.k);
        String type = parseFromJson.getType();
        if (type.equals(j.netflow.name())) {
            i iVar = new i(parseFromJson, uiDevice);
            u.a().a(this.l, j.netflow);
            iVar.initEnv();
            iVar.a(parseFromJson.getNetflow());
        } else if (!type.equals(j.loginview.name())) {
            if (type.equals(j.order.name())) {
                a(parseFromJson, cVar);
            } else if (!type.equals(j.comment.name())) {
            }
        }
    }

    public void a(String str) {
        this.k = str;
    }

    public UiDevice b() {
        return this.m;
    }

    public void b(UiDevice uiDevice) {
        this.m = uiDevice;
    }

    public b c(UiDevice uiDevice) throws UiObjectNotFoundException, c {
        String str = (String) new i(this.k, true).c();
        if (this.m == null) {
            this.m = uiDevice;
        }
        System.out.println(">>>>: \n" + str);
        a(uiDevice, str);
        return null;
    }

    public void d(UiDevice uiDevice) throws UiObjectNotFoundException, c {
        e eVar = new e(uiDevice);
        eVar.doOpenChuanhao(uiDevice);
        com.sutuijingling.utils.c.e();
        eVar.doChangeChuanhao();
        f(uiDevice);
        com.sutuijingling.utils.c.c();
        a.a();
    }

    public void e(UiDevice uiDevice) throws UiObjectNotFoundException, c {
        e eVar = new e(uiDevice);
        uiDevice.pressBack();
        com.sutuijingling.utils.c.b();
        uiDevice.pressHome();
        com.sutuijingling.utils.c.b();
        uiDevice.pressBack();
        a.k();
        com.sutuijingling.utils.c.e();
        UiObject findText = eVar.findText("开始执行", 2);
        for (int i = 0; i < 5 && !findText.exists(); i++) {
            com.sutuijingling.utils.c.e();
            UiScrollable findScroll = eVar.findScroll();
            if (findScroll.exists()) {
                findScroll.setAsVerticalList();
                findScroll.scrollBackward();
            }
            com.sutuijingling.utils.c.c();
            findText = eVar.findText("开始执行", 1);
        }
        if (findText.exists()) {
            com.sutuijingling.utils.c.c(findText);
        }
    }

    public void f(UiDevice uiDevice) {
        System.out.println(" return back to home ... ");
        uiDevice.pressBack();
        com.sutuijingling.utils.c.b();
        uiDevice.pressBack();
        com.sutuijingling.utils.c.b();
        uiDevice.pressHome();
        com.sutuijingling.utils.c.b();
        uiDevice.pressBack();
        com.sutuijingling.utils.c.b();
        uiDevice.pressBack();
    }

    public long g(UiDevice uiDevice) {
        File file = new File("/data/local/tmp/missionid.txt");
        if (!file.exists()) {
            file = new File("/data/local/tmp/missionId.txt");
        }
        if (!file.exists()) {
            return 0;
        }
        try {
            String trim = org.apache.commons.io.b.b(file).trim();
            System.out.println(" read mission id txt:" + trim);
            Long a = NumberUtil.a(trim, Long.valueOf(0));
            return (a == null || a.longValue() <= 0) ? 0 : a.longValue();
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public void g() throws c, UiObjectNotFoundException {
        w b = w.b();
        if (b == null) {
            com.sutuijingling.utils.b.b("no task recover...", new Object[0]);
            throw new c(HttpStatus.SC_NOT_FOUND, " no task to recover...");
        }
        long e = b.e();
        int f = b.f();
        if (e < 0) {
            throw new c(HttpStatus.SC_NOT_FOUND, " no task to recover...");
        } else if (((Boolean) new k(Long.valueOf(e), c()).c()).booleanValue()) {
            new i(a(Long.valueOf(e)).getSearchBean(), this.m).b(f);
            switch (f) {
            }
        } else {
            throw new c(HttpStatus.SC_BAD_REQUEST, " not to recover..");
        }
    }

    public void h() throws UiObjectNotFoundException, c {
        w.c();
        i();
    }

    public void h(UiDevice uiDevice) throws c, UiObjectNotFoundException {
        a(uiDevice, new c());
    }

    public void i() throws UiObjectNotFoundException, c {
        TopBean parseFromJson;
        Exception e;
        long j;
        String str;
        TopBean topBean;
        int i;
        AccountBean accountBean;
        w wVar = null;
        com.sutuijingling.utils.b.b("this key:" + this.k, new Object[0]);
        int i2 = 0;
        String str2 = "";
        if (-1 > 0) {
            try {
                parseFromJson = TopBean.parseFromJson((String) new r(0, -1, this.k).c(), this.k);
                wVar.d();
                i2 = wVar.f();
                com.sutuijingling.utils.b.b(" parse from prev res :: " + wVar, new Object[0]);
            } catch (c e2) {
                e = e2;
                r7 = str2;
                j = 0;
                if (e.a() != HttpStatus.SC_NOT_FOUND) {
                    System.out.println("error code:" + e.a());
                    a.e("");
                } else {
                    System.out.println("error code:" + e.a());
                    a.e("");
                }
                if (e.a() == 9003) {
                    String str3;
                    a(9003);
                    new o(this.k, str3, e.b()).c();
                }
                a(this.m, e, j, e.a(), e.b(), str3);
                w.c();
                throw e;
            } catch (Exception e3) {
                e = e3;
                str3 = str2;
                j = 0;
                e.printStackTrace();
                a(this.m, e, j, HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), str3);
                w.c();
                a.e("");
                throw new c(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
            }
        }
        parseFromJson = null;
        com.sutuijingling.utils.b.b(" might from prev bean :" + parseFromJson, new Object[0]);
        long j2 = j();
        if (parseFromJson == null) {
            AccountBean f = f();
            if (f == null) {
                com.sutuijingling.utils.b.b(" acc bean is null !!!  ", new Object[0]);
                com.sutuijingling.utils.c.f();
                return;
            }
            if (f != null) {
                if (!org.apache.commons.lang.c.a(f.getRealname())) {
                    f.encryptRealName(this.k);
                }
                str2 = f.getRawusername();
            }
            if (org.apache.commons.lang.c.a(str2)) {
                com.sutuijingling.utils.b.b(" wangawng is empty !!!  ", new Object[0]);
                com.sutuijingling.utils.c.f();
                return;
            }
            TopBean parseFromJson2 = TopBean.parseFromJson((String) new s(this.k, 0, true, str2, j2).c(), this.k);
            str = str2;
            topBean = parseFromJson2;
            i = 0;
            accountBean = f;
        } else {
            i = i2;
            topBean = parseFromJson;
            accountBean = null;
            str = str2;
        }
        try {
            com.sutuijingling.utils.b.b(" fetch from server...    :" + topBean, new Object[0]);
            if (topBean == null) {
                com.sutuijingling.utils.b.b(" recent bean is null ", new Object[0]);
                com.sutuijingling.utils.c.f();
                return;
            }
            switch (topBean.getCode()) {
                case 7008:
                    com.sutuijingling.utils.b.b(" current ip not available any more, we have to change", new Object[0]);
                    a((int) HttpStatus.SC_BAD_REQUEST);
                    com.sutuijingling.utils.c.f();
                    return;
                case 7009:
                    com.sutuijingling.utils.b.b(" No more mission, wait the seller to publish..", new Object[0]);
                    a((int) HttpStatus.SC_BAD_REQUEST);
                    com.sutuijingling.utils.c.f();
                    return;
                case 8001:
                    a(j2, this.k);
                    return;
                default:
                    if (topBean.getTaskId() <= 0) {
                        com.sutuijingling.utils.b.b(" Task id is null..", new Object[0]);
                        a((int) HttpStatus.SC_BAD_REQUEST);
                        com.sutuijingling.utils.c.f();
                        return;
                    }
                    a((int) HttpStatus.SC_UNAUTHORIZED);
                    if (org.apache.commons.lang.c.a(accountBean.getRealname())) {
                        accountBean.decryptRealName(this.k);
                    }
                    topBean.setAcc(accountBean);
                    i iVar = new i(topBean, this.m);
                    iVar.K();
                    this.j = topBean.getType();
                    long taskId = topBean.getTaskId();
                    if (topBean.getSearchBean() != null && topBean.getSearchBean().isTaoqianggou()) {
                        new j(topBean, this.m).V();
                    } else if (topBean.getSearchBean() != null && topBean.getSearchBean().isTaobaoke()) {
                        iVar.O();
                    } else if (topBean.getSearchBean() == null || !topBean.getSearchBean().isJustTklInto()) {
                        iVar.c(i);
                    } else {
                        iVar.P();
                    }
                    boolean d = d();
                    boolean e4 = e();
                    if ((topBean.getSearchBean() == null || !topBean.getSearchBean().isZhanXian()) && !topBean.getSearchBean().isTaobaoke()) {
                        k();
                    }
                    w.c();
                    a.b("rm -r /data/local/tmp/storeup.txt");
                    a.b("rm -r /data/local/tmp/shopstore");
                    if (org.apache.commons.lang.c.a(str)) {
                        new p(Long.valueOf(taskId), this.k).c();
                    } else {
                        new p(Long.valueOf(taskId), this.k, str, Long.valueOf(j2), d, false, e4).c();
                    }
                    a.e("");
                    f(this.m);
                    return;
            }
        } catch (c e5) {
            e = e5;
            str3 = str;
            j = 0;
            if (e.a() != HttpStatus.SC_NOT_FOUND) {
                System.out.println("error code:" + e.a());
                a.e("");
            } else {
                System.out.println("error code:" + e.a());
                a.e("");
            }
            if (e.a() == 9003) {
                a(9003);
                new o(this.k, str3, e.b()).c();
            }
            a(this.m, e, j, e.a(), e.b(), str3);
            w.c();
            throw e;
        } catch (Exception e6) {
            e = e6;
            str3 = str;
            j = 0;
            e.printStackTrace();
            a(this.m, e, j, HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), str3);
            w.c();
            a.e("");
            throw new c(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        }
    }
}
